//
//  FIDCheckMap.m
//  Kadima
//
//  Created by Fidetro on 2016/11/21.
//  Copyright © 2016年 Fidetro. All rights reserved.
//



#import "FIDCheckMap.h"


@implementation CheckMapModel

- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate{
    
    if ([super init]) {
        
        self.latitude = coordinate.latitude;
        self.longitude = coordinate.longitude;
        
    }
    
    return self;
    
}

/**
 转换成高德地图经纬度的方法
 
 @return 返回高德地图经纬度
 */
- (CLLocationCoordinate2D)aLiCoordinate{
    
//    CLLocationCoordinate2D aliAmapcoord  =  AMapCoordinateConvert(CLLocationCoordinate2DMake(self.latitude ,self.longitude),AMapCoordinateTypeGPS);
    CLLocationCoordinate2D aliAmapcoord  = CLLocationCoordinate2DMake(self.latitude, self.longitude);
    return aliAmapcoord;
}


@end

@implementation FIDCheckMap

+ (void)callMapWithpositionModel:(CheckMapModel *)mapModel completion:(void(^)(UIAlertController *alertController))completion{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"导航"
                                                                   message:@"请选择"
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    NSArray *mapArray = [FIDCheckMap getInstalledMapAppWithpositionModel:mapModel];
    for (NSDictionary *dic in mapArray) {
        if ([dic[@"url"] isKindOfClass:[NSString class]]) {
            UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:dic[@"title"]
                                                                    style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:dic[@"url"]]];
                                                                  }];
            
            [alert addAction:defaultAction];
        }else{
            
            UIAlertAction *appleMapAction = [UIAlertAction actionWithTitle:@"苹果地图"
                                                                     style:UIAlertActionStyleDefault
                                                                   handler:^(UIAlertAction * action) {
                                                                       
                                                                       [MKMapItem openMapsWithItems:dic[@"url"][@"mapItems"] launchOptions:dic[@"url"][@"launchOptions"]];
                                                                   }];
            [alert addAction:appleMapAction];
            
            
        }
        
        
    }
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action) {
                                                             
                                                         }];
    
    [alert addAction:cancelAction];
    
    if (completion) {
        completion(alert);
    }
    
}
/**
 传入结束的经纬度返回可以调用原生的地图进行导航
 
 @param mapModel 服务器返回的model
 @return 返回可以已经安装后的地图。@[@[@{@"title":@"百度地图",@"url":@"弹出第三方地图的url"}]]
 */
+ (NSArray *)getInstalledMapAppWithpositionModel:(CheckMapModel *)mapModel{
    
    NSMutableArray *maps = [NSMutableArray array];
    float latitude = mapModel.latitude;//火星坐标
    float longitude = mapModel.longitude;//火星坐标
    CLLocationCoordinate2D aLiCoordinate = [mapModel aLiCoordinate];//阿里坐标
    
    
    
    //百度地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];
        baiduMapDic[@"title"] = @"百度地图";
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%lf,%lf|name=北京&mode=driving&coord_type=wgs84",latitude,longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        baiduMapDic[@"url"] = urlString;
        [maps addObject:baiduMapDic];
    }
    
    //高德地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
        gaodeMapDic[@"title"] = @"高德地图";
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",@"导航功能",@"nav123456",aLiCoordinate.latitude,aLiCoordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        gaodeMapDic[@"url"] = urlString;
        [maps addObject:gaodeMapDic];
    }
    
    //谷歌地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
        NSMutableDictionary *googleMapDic = [NSMutableDictionary dictionary];
        googleMapDic[@"title"] = @"谷歌地图";
        NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%lf,%lf&directionsmode=driving",@"菠萝鱼",@"nav123456",latitude, longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        googleMapDic[@"url"] = urlString;
        [maps addObject:googleMapDic];
    }
    //腾讯地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
        NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
        qqMapDic[@"title"] = @"腾讯地图";
        NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%lf,%lf&to=终点&coord_type=1&policy=0&referer=菠萝鱼",aLiCoordinate.latitude, aLiCoordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        qqMapDic[@"url"] = urlString;
        [maps addObject:qqMapDic];
    }
    
    //苹果地图
    MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:aLiCoordinate addressDictionary:nil]];
    NSArray *items = @[currentLoc,toLocation];
    NSDictionary *dic = @{
                          MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
                          MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
                          MKLaunchOptionsShowsTrafficKey : @(YES)
                          };
    
    NSMutableDictionary *appleMapDic = [NSMutableDictionary dictionary];
    appleMapDic[@"title"] = @"苹果地图";
    appleMapDic[@"url"] = @{@"mapItems":items,@"launchOptions":dic};
    
    [maps addObject:appleMapDic];
    
    return maps;
    
}


@end

